﻿using System.Collections.Generic;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Repositorys
{
    public class OssRoleRouteRepository : SqlServerRespository<OssRoleRouteEntity>, IOssRoleRouteRepository
    {
        public OssRoleRouteRepository()
        {
            Context = new OssContext();
        }


        public IEnumerable<OssRoleRouteEntity> GetOssRoleRoutesByRoleid(long roleid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<OssRoleRouteEntity>();
            return
                GetList(
                    $"SELECT {querysql} FROM oss_role_page WHERE roleid=@roleid",
                    new {roleid});
        }

        public IEnumerable<OssUserRouteEntity> GetOssRoleRoutesByUserid(long userid)
        {
            var list =
                DapperHelper.GetList<OssUserRouteEntity>(
                    @"SELECT a.userid AS UserId,b.pageroute AS RoutePath FROM oss_user_roles a
                    INNER JOIN oss_role_page b ON a.roleid=b.roleid
                    WHERE a.userid =@userid
                    GROUP BY a.userid,b.pageroute", Context,
                    new {userid});

            return list;
        }

        public void AddOssRoleRoute(OssRoleRouteEntity entity, UnitOfWork unitOfWork = null)
        {
            Add(entity, unitOfWork);
        }

        public void DeleteRouteByRoleid(long roleid, UnitOfWork unit = null)
        {
            DapperHelper.Execute(@"Delete from oss_role_page WHERE roleid=@roleid", Context, unit, new {roleid});
        }
    }
}